/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package publicRelations.beans;

import candidate.beans.*;
import entities.Language;
import entities.PublicRelations;
import entities.User;
import entities.UserConfiguration;
import facades.LanguageFacade;
import facades.PublicRelationsFacade;
import facades.UserConfigurationFacade;
import facades.UserFacade;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.event.Event;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import qualifiers.EditedEntityEvent;
import qualifiers.LoggedIn;
import qualifiers.PreEditEntityEvent;
import util.Sha256Encrypter;

/**
 *
 * @author Alvaro
 */
@Named
@SessionScoped
public class PreferencesPublicRelationsBean implements Serializable {
    
    @Inject
    @LoggedIn
    private PublicRelations publicRelations;
    @Inject
    private UserConfigurationFacade userConfigurationFacade;
    @Inject
    private UserFacade userFacade;
    @Inject
    private PublicRelationsFacade publicRelationsFacade;
    @Inject
    private LanguageFacade languageFacade;
    @Inject
    @PreEditEntityEvent
    private Event<UserConfiguration> preEditUserConfigurationEvent;
    @Inject
    @EditedEntityEvent
    private Event<UserConfiguration> editUserConfigurationEvent;
    @Inject
    @PreEditEntityEvent
    private Event<User> preEditUserEvent;
    @Inject
    @EditedEntityEvent
    private Event<User> editUserEvent;
    private Language languageCollector;
    
    
    @PostConstruct
    public void init(){
        languageCollector=getSelectedLanguage(getPublicRelations());
    }

    public Language getSelectedLanguage(PublicRelations c){
        if(c.getUser().getUserConfiguration()!=null){
            return c.getUser().getUserConfiguration().getLanguage();
        }
        else{
            Language language=languageFacade.find(FacesContext.getCurrentInstance().getViewRoot().getLocale());
            if(language==null){
                language=languageFacade.getEnglish();
            }
            return language;
        }
    }
    
    public void save(){
        preEditUserConfigurationEvent.fire(getPublicRelations().getUser().getUserConfiguration());
        getPublicRelations().getUser().getUserConfiguration().setLanguage(languageCollector);
        userConfigurationFacade.edit(getPublicRelations().getUser().getUserConfiguration());
        editUserConfigurationEvent.fire(getPublicRelations().getUser().getUserConfiguration());
        FacesContext context=FacesContext.getCurrentInstance();
        Locale locale=context.getViewRoot().getLocale();
        ResourceBundle.clearCache();
        ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
        String successMessage=rb.getString("UpdatedSuccessfully");
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, successMessage, successMessage);  
        context.addMessage(null, message); 
        context.renderResponse();
        //return "/views/publicRelations/ConfigurationPublicRelationsView?faces-redirect=true";
    }
    
    public void changePassword(){
        preEditUserEvent.fire(getPublicRelations().getUser());
        try {
            getPublicRelations().getUser().setPassword(Sha256Encrypter.getSha256(getPublicRelations().getUser().getPassword()));
            userFacade.edit(getPublicRelations().getUser());
            editUserEvent.fire(getPublicRelations().getUser());
            FacesContext context=FacesContext.getCurrentInstance();
            Locale locale=context.getViewRoot().getLocale();
            ResourceBundle.clearCache();
            ResourceBundle rb = ResourceBundle.getBundle("messages", locale);
            String successMessage=rb.getString("UpdatedSuccessfully");
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, successMessage, successMessage);  
            context.addMessage(null, message); 
            context.renderResponse();
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(PreferencesCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(PreferencesCandidateBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    

    /**
     * @return the languageCollector
     */
    public Language getLanguageCollector() {
        return languageCollector;
    }

    /**
     * @param languageCollector the languageCollector to set
     */
    public void setLanguageCollector(Language languageCollector) {
        this.languageCollector = languageCollector;
    }

    /**
     * @return the publicRelations
     */
    public PublicRelations getPublicRelations() {
        return publicRelations;
    }

    /**
     * @param publicRelations the publicRelations to set
     */
    public void setPublicRelations(PublicRelations publicRelations) {
        this.publicRelations = publicRelations;
    }
}
